<?php
namespace src;
use classlib\input;
use src\logic\Cadmin;
use classlib\core as C;
use classlib\core\controller;
use classlib\validator\auditor;
class Cstarlv extends controller {
	public function main() {
		$view  = C\view::inst('extjs');
		Cadmin::chkaccess($view);
		$view->assign_by_ref('title', Cadmin::$name);
		$view->display('live/starlv.ext.js');
	}
	
	/**
	 * @desc    保存等级资料信息
	 * @doc     编辑/添加等级
	 * @access  需要授权 
	 */
	public function dosave() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		$filter = new \classlib\validator\auditor();
		$lang   = C\lang::inst('adm');
		$filter->lang($lang);
		$data   = array();
		try {
			$salary = $filter->reset('salary')->required()->run();
			if ($salary) {
				$list   = @explode('>=', $salary);
				$salary = array();
				foreach ($list as $str) {
					$str = trim($str);
					if (!$str) {
						continue;
					}
					list($hour, $value) = explode(':', $str);
					if ($hour < 0 || $value < 1) {
						continue;
					}
					$salary[] = array('hour'=>intval($hour), 'salary'=>floatval($value));
				}
			}
			$data['salary'] = empty($salary)? '':json_encode($salary);
			$data['name']   = $filter->reset('name')->required()->minlen(1)->run();
			$data['star']   = $filter->reset('star')->alphanum()->minlen(1)->run();
			$data['ratio']  = $filter->reset('ratio')->numeric()->between(0, 1)->run();
		} catch (\classlib\validator\AuditorException $e) {
			$view->assign_error($e->getField(), $e->getMessage());
		} catch (\Exception $e) {
			$view->assign_error($e->getCode(), $e->getMessage());
		}
		$id    = (int)input::get_post('id');
		$orm   = C\model::load('pro.inc.star');
		$data['stime'] = C\config::$nowtime;
		$data['ip']    = C\get_ip();
		($id > 0)? $orm->save($id, $data):$orm->new_one($data);
		Cadmin::log2db(C\view::inst('xml')->array2xml($data));
		$view->display();
	}
	
	/**
	 * @desc    删除等级配置
	 * @doc     删除记录
	 * @access  需要授权
	 */
	public function dodelete() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		$lang   = C\lang::inst('adm');
		$ids    = auditor::intstr2arr('ids');
		$orm    = C\model::load('pro.inc.star');
		$aorm   = C\model::load('pro.pl.anchor');
		foreach ($ids as $id) {
			$total = $aorm->get_total(array('starlv'=>$item['id']));
			if ($total > 0) {
				$view->assign_error(40008, $lang->common('40008'));
			}
			$data  = $orm->get_one($id);
			if ($data) {
				Cadmin::log2db(C\view::inst('xml')->array2xml($data));
				$orm->delete($id);
			}
		}
		$view->display();
	}
	
	/**
	 * 获取用户资料列表
	 */
	public function dolist() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		list($start, $limit, $sort) = Cadmin::offset();
		$orm    = C\model::load('pro.inc.star');
		$total  = $orm->get_total($where);
		$list   = array();
		$where  = array();
		if ($total > 0) {
			$list = $orm->get_list($start, $limit, $where, '*', $sort);
			$aorm = C\model::load('pro.pl.anchor');
			foreach ($list as &$item) {
				$item['count'] = $aorm->get_total(array('starlv'=>$item['id']));
				$item['stime'] = date('Y-m-d H:i:s', $item['stime']);
			}
		}
		$view->assign_by_ref('total', $total);
		$view->assign_by_ref('dataset', $list);
		$view->display();
	}
}